package com.sunline.poc.oozie.test.demo;

import org.apache.oozie.client.AuthOozieClient;
import org.apache.oozie.client.OozieClientException;
import org.apache.oozie.client.WorkflowAction;
import org.apache.oozie.client.WorkflowJob;

import java.util.List;
import java.util.Properties;

public class JavaWorkflowDemo1 {
    private static String oozieURL = "http://n1:11000/oozie";
    public static void main(String[] args) {
        System.setProperty("java.security.krb5.conf", "D:\\Temp\\krb5.conf");
        System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
        System.setProperty("ssun.security.jgss.debug", "true"); //Kerberos Debug模式
        System.setProperty("java.security.auth.login.config", "D:\\Temp\\oozie-login-clientA.conf");
        AuthOozieClient oozieClient = new AuthOozieClient(oozieURL, AuthOozieClient.AuthType.KERBEROS.name());
        oozieClient.setDebugMode(1);
        try {
            Properties properties = oozieClient.createConfiguration();
            properties.put("oozie.use.system.libpath", "True");
            properties.put("nameNode", "hdfs://n2:8020");
            properties.put("jobTracker", "n1:8032");
            properties.put("mainClass", "org.apache.hadoop.examples.QuasiMonteCarlo");
            properties.put("arg1", "10");
            properties.put("arg2", "10");
            properties.put("javaOpts", "-Xmx200m");
            properties.put("oozie.libpath", "${nameNode}/clientA/jars/");
            properties.put("oozie.wf.application.path", "${nameNode}/user/clientA/oozie/javaaction");
            properties.put("user.name","clientA");
            //运行workflow
            String jobid = oozieClient.run(properties);
            System.out.println("jobid="+jobid);
            //等待10s
            Thread.sleep(10000l);
            //根据workflow id获取作业运行情况
            WorkflowJob workflowJob = oozieClient.getJobInfo(jobid);
            //获取作业日志
            System.out.println(oozieClient.getJobLog(jobid));
            //获取workflow中所有ACTION
            List<WorkflowAction> list = workflowJob.getActions();
            for (WorkflowAction action : list) {
                //输出每个Action的 Appid 即Yarn的Application ID
                System.out.println(action.getExternalId());
            }
            //杀掉作业
//            oozieClient.kill(jobid);
        } catch (OozieClientException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
